#include "configservice.h"
#include <qsqlquery.h>
#include <QDateTime>
#include <autoDatabase.h>

ConfigService::ConfigService()
{

}

bool ConfigService::createTable()
{
    // 获取数据连接
    AutoDatabase autoDb;

    // 数据库查询
    QSqlQuery query(autoDb.getDb());
    return query.exec(QString("CREATE TABLE IF NOT EXISTS t_config("
                              "`id` varchar(32) NOT NULL COMMENT '配置编码',"
                              "`name` varchar(32) NOT NULL COMMENT '配置名称',"
                              "`value` varchar(255) DEFAULT NULL COMMENT '配置数值',"
                              "`enabled` int DEFAULT 1 COMMENT '启用状态',"
                              "`createTime` datetime default now() COMMENT '配置时间',"
                              "`reserver1` varchar(128) default NULL COMMENT '保留字段',"
                              "`reserver2` varchar(128) default NULL COMMENT '保留字段',"
                              "primary key(`id`)"
                              ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统配置';"));
}

bool ConfigService::add(ConfigInfo config)
{
    // 获取数据连接
    AutoDatabase autoDb;

    // 数据库查询
    QString sql = "insert into t_config(`id`, `name`, `value`, `enabled`, `createTime`)"
                  "values(:id, :name, :value, :enabled, :createTime)";
    QSqlQuery query(autoDb.getDb());
    query.prepare(sql);
    query.bindValue(":id", config.id);
    query.bindValue(":name", config.name);
    query.bindValue(":value", config.value);
    query.bindValue(":enabled", config.enabled);
    query.bindValue(":createTime", config.createTime);
    return query.exec();
}

bool ConfigService::remove(QString id)
{
    // 获取数据连接
    AutoDatabase autoDb;

    // 移除数据记录
    QString sql = "delete from t_config where `id`=:id";
    QSqlQuery query(autoDb.getDb());
    query.prepare(sql);
    query.bindValue(":id", id);
    return query.exec();
}

bool ConfigService::update(ConfigInfo config)
{
    // 获取数据连接
    AutoDatabase autoDb;

    // 更新数据记录
    QString sql = "update t_config set `name`=:name, `value`=:value, "
                  "`enabled`=:enabled, `createTime`=:createTime"
                  "where id=:id";
    QSqlQuery query(autoDb.getDb());
    query.prepare(sql);
    query.bindValue(":name", config.name);
    query.bindValue(":value", config.value);
    query.bindValue(":enabled", config.enabled);
    query.bindValue(":createTime", config.createTime);
    query.bindValue(":id", config.id);
    return query.exec();
}

bool ConfigService::find(ConfigInfo config)
{
    // 获取数据连接
    AutoDatabase autoDb;

    // 查询数据记录
    QString sql = "select * from t_config where `id`=:id";
    QSqlQuery query(autoDb.getDb());
    query.prepare(sql);
    query.bindValue(":id", config.id);

    // 打印查询结果
    if (query.exec() && query.next()) {
        config.id = query.value("id").toString();
        config.name = query.value("name").toString();
        config.value = query.value("value").toString();
        config.enabled = query.value("enabled").toInt();
        config.createTime = query.value("createTime").toDateTime().toString("yyyy-MM-dd hh:mm:ss");
        return true;
    }
    return false;
}

bool ConfigService::findAll(QVector<ConfigInfo>& configs)
{
    // 获取数据连接
    AutoDatabase autoDb;

    // 查询数据记录
    QString sql = "select * from t_config";
    QSqlQuery query(autoDb.getDb());
    query.prepare(sql);

    // 执行结果查询
    bool success = query.exec();
    if (success) {
        // 打印查询结果
        while (query.next()) {
            ConfigInfo config;
            config.id = query.value("id").toString();
            config.name = query.value("name").toString();
            config.value = query.value("value").toString();
            config.enabled = query.value("enabled").toInt();
            config.createTime = query.value("createTime").toDateTime().toString("yyyy-MM-dd hh:mm:ss");
            configs.push_back(config);
        }
    }
    return success;
}
